<template>
  <div class="app-container" style="background: #f3f4f6;flex: 1;">
    <div class="teacher_search">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="培训名称" prop="trainingTitle">
          <el-input
            v-model="queryParams.trainingTitle"
            placeholder="请输入培训名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="培训类型" prop="trainingType">
          <el-select v-model="queryParams.trainingType" placeholder="请选择培训类型" clearable>
            <el-option
              v-for="dict in dict.type.px_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="trainingType">
          <el-select v-model="queryParams.trainingStatus" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in statusLi"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['classai:teacher:add']"
        >新增培训</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['classai:teacher:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['classai:teacher:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['classai:teacher:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="trainingList" @selection-change="handleSelectionChange" class="teacher_list">
      <el-table-column label="培训名称" align="center" prop="trainingTitle" />
      <el-table-column label="培训类型" align="center" prop="trainingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.px_type" :value="scope.row.trainingType"/>
        </template>
      </el-table-column>
      <el-table-column label="培训开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span >{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地点" align="center" show-overflow-tooltip prop="trainingAddress">
        <template slot-scope="scope">
          <span >{{scope.row.trainingAddress ?scope.row.trainingAddress:'--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span class="button-status" :class="scope.row.state==='未开始'?'status4':scope.row.state==='进行中'?'status3':scope.row.state==='已结束'?'status2':''">{{scope.row.state}}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="培训备注" align="center" prop="trainingNotes" />-->
      <!--      <el-table-column label="培训学时" align="center" prop="trainingHours" />-->
      <!--      <el-table-column label="培训学分" align="center" prop="trainingScore" />-->
      <!--      -->
      <!--      -->
      <!--      <el-table-column label="培训结束时间" align="center" prop="endTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="附件地址" align="center" prop="attachmentUrl" />-->
      <!--      <el-table-column label="培训地址" align="center" show-overflow-tooltip prop="trainingAddress" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['classai:training:edit']"
          >
            <img src="@/assets/images/teacher_edit.png" style="width:16px;height:16px;" alt="">
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['classai:training:edit']"
          >
            <img src="@/assets/images/teacher_detail.png" style="width:12px;height:16px;" alt="">
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['classai:training:remove']"
          >
            <img src="@/assets/images/teacher_del.png" style="width:14px;height:16px;" alt="">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination_d">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import {addTraining, delTraining, getTraining, listTraining, updateTraining} from "@/api/classai/training";

export default {
  name: "Training ",
  dicts: ['px_type',],
  data() {
    return {
      statusLi:[
        {
          value:0,
          label:'未开始',
        },
        {
          value:1,
          label:'进行中',
        },
        {
          value:2,
          label:'已结束',
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训信息表格数据
      trainingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainingTitle: null,
        trainingType: null,
        trainingStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训信息列表 */
    getList() {
      this.loading = true;
      listTraining(this.queryParams).then(response => {
        this.trainingList = response.rows;
        this.trainingList.forEach(item =>{
          item.state = this.getStatus(item.startTime,item.endTime)
        })
        // this.trainingList.status = this.getStatus(startTime,endTime)
        this.total = response.total;
        this.loading = false;
      });
    },
    getStatus(startTime, endTime) {
      const now = new Date().getTime(); // 当前时间戳
      const start = new Date(startTime).getTime();
      const end = new Date(endTime).getTime();

      if (now < start) {
        return '未开始';
      } else if (now >= start && now <= end) {
        return '进行中';
      } else {
        return '已结束';
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainingTitle: null,
        trainingNotes: null,
        trainingHours: null,
        trainingScore: null,
        trainingType: null,
        startTime: null,
        endTime: null,
        createTime: null,
        updateTime: null,
        attachmentUrl: null,
        trainingAddress: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.trainingTitle = null,
      this.queryParams.trainingType = null,
      this.queryParams.trainingStatus = null,

      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加培训信息";
      this.$router.push("/pxmanage/trainingPublish")
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      // getTraining(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改培训信息";
      // });
      this.$router.push(`/pxmanage/EditTraining?id=${row.id}`)
    },
    handleDetail(row) {
      this.$router.push(`/pxmanage/trainingDetail?id=${row.id}`)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTraining(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTraining(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训信息编号为"' + ids + '"的数据项？').then(function() {
        return delTraining(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('classai/training/export', {
        ...this.queryParams
      }, `training_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.teacher_list {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  ::v-deep {
    .el-tag {
      border-radius: 15px;
      border: none;
      min-width: 50px;
    }
    .teacher_gender {
      .el-tag {
        background: transparent;
        color: #333;
      }
    }
    .has-gutter {
      background: #F3F4F6;
      height: 56px;
      .el-table__cell {
        background: #F3F4F6;
      }
    }
  }
}
.teacher_search {
  margin-bottom: 10px;
  padding: 15px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  ::v-deep {
    .el-form-item {
      margin-bottom: 0;
    }
  }
}
.pagination_d {
  margin-top: 10px;
  padding: 15px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  ::v-deep {
    .pagination-container {
      margin-top: 0;
      background: transparent;
    }
  }
}

.button-status{
  font-size: 12px;
  padding: 4px 8px;
  box-sizing: border-box;
  border-radius: 15px;
}
.status0{
  color: rgb(245, 108, 108);
  background: rgb(254, 240, 240);
}
.status1{
  color: #854D0E;
  background: #FEF9C3;
}
.status2{
  color: rgb(144, 147, 153);
  background: rgb(244, 244, 245);
}
.status3{
  color: #166534;
  background: #DCFCE7;
}
.status4{
  color: #1E40AF;
  background: #DBEAFE;
}
</style>
